<?php


namespace app\model;

use app\common\library\Translation;
use think\Model;

class RemarkModel extends Model
{
    protected $table = "store_remark";

    /**
     * @param $data
     * @param $table_name
     * @param $table_id
     * @return int|string
     * @notes 备注 中俄换算
     * @author: dongdong
     * @date: 2024/12/31 11:21
     */
    public static function remark_id($str, $table_name, $table_id)
    {
        if(!empty($str)){
            if (!is_numeric($str)) {
                $remark = self::translationAction($str);
                $d['remark'] = $remark['zh'];
                $d['remark_ru'] = $remark['ru'];
                $d['table_name'] = $table_name;
                $d['table_id'] = $table_id;
                return self::insertGetId($d);
            }else{
                $d['remark'] = $str;
                $d['remark_ru'] = $str;
                $d['table_name'] = $table_name;
                $d['table_id'] = $table_id;
                return self::insertGetId($d);
            }
        }else{
            return 0;
        }
    }

    /**
     * @param $name
     * @return array
     * @notes 翻译方法
     * @author: dongdong
     * @date: 2024/12/31 11:17
     */
    public static function translationAction($name)
    {
        if(!empty($name)) {
            if(is_numeric($name)){
                return ['zh' => $name, 'ru' => $name];
            }else{
                $Translation = new Translation();
                $wenzi = $Translation->languageDetect($name);
                $language = $wenzi['Lang'];
                if ($language == 'zh') {// 中 - 俄
                    $translated = $Translation->textTranslate($name, $Source = 'zh', $Target = 'ru');
                    $name_ru = $translated['TargetText'];
                } else if ($language == 'ru') {// 俄 - 中
                    $translated = $Translation->textTranslate($name, $Source = 'ru', $Target = 'zh');
                    $name_ru = $name;
                    $name = $translated['TargetText'];
                } else {
                    // 输入其他文字时，中俄相同
                    $name_ru = $name;
                }
                return ['zh' => $name, 'ru' => $name_ru];
            }
        }else{
            return ['zh' => '', 'ru' => ''];
        }
    }


    /**
     * @param $id
     * @return RemarkModel|array|mixed|Model|null
     * @notes 备注
     * @author: dongdong
     * @date: 2025/1/8 14:49
     */
    public function info($id){
        try {
            return self::where(["id"=>$id])->field("id,remark,remark_ru")->find();
        }catch (\Exception $e){
            return [];
        }
    }

}